import { ref } from 'vue'

export interface AggregateInfo {
  AggregateId: number
  AggregatePackage: string
  Comment: string
  CompanyName: string
  Config: { key: string; value: string; comment: string }[]
  Content: string
  ModelVos: any[]
  Name: string
  ProjectName: string
}

export function useGetAggregateInfo(baseUrl: string, params: { projectId: number; aggregateId: number }) {
  const info = ref<AggregateInfo | null>(null)
  const loading = ref(false)
  const error = ref<string | null>(null)

  async function fetchInfo() {
    loading.value = true
    error.value = null
    try {
      const url = `${baseUrl}/api/aggregate/info?projectId=${params.projectId}&aggregateId=${params.aggregateId}`
      const res = await fetch(url)
      if (!res.ok) throw new Error('网络错误')
      info.value = await res.json()
    } catch (e: any) {
      error.value = e.message || '未知错误'
    } finally {
      loading.value = false
    }
  }

  return { info, loading, error, fetchInfo }
}
